package com.frame;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import com.cchess.CchessGamePanel;
import com.cchess.CchessRes;
import com.cchess.CchessSetting;
import com.openningbook.BookItem;
import com.openningbook.BookItemNode;
import com.openningbook.MyTreeCellRenderer;

import xqwlight.Position;

public class ViewBookFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3929254769890390171L;
	private JPanel contentPane;
	private CchessGamePanel panel;
	private CchessSetting set;
	private CchessRes res;
	private JSplitPane splitPane;
	private Position pos;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		ViewBookFrame gf = new ViewBookFrame();
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (size.width - gf.getWidth()) / 2;
		int y = (size.height - gf.getHeight()) / 2;
		gf.setLocation(x, y);
		gf.setVisible(true);
	}

	/**
	 * Create the frame.
	 */
	public ViewBookFrame() {
		setTitle("\u5F00\u5C40\u5E93\u7740\u6CD5\u67E5\u770B");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 723, 656);

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		splitPane = new JSplitPane();
		pos = new Position();
		res = new CchessRes();
		set = new CchessSetting();
		res.loadRes();
		pos.fromFen(Position.STARTUP_FEN[0]);
		contentPane.add(splitPane, BorderLayout.CENTER);
		panel = new CchessGamePanel(pos, res, set);
		splitPane.setLeftComponent(panel);
		splitPane.setDividerLocation(530);
		JScrollPane scrollPane = new JScrollPane();
		splitPane.setRightComponent(scrollPane);

		final JTree tree = new JTree(createModel());
		tree.setCellRenderer(new MyTreeCellRenderer());

		tree.addTreeExpansionListener(new TreeExpansionListener() {
			@Override
			public void treeExpanded(TreeExpansionEvent e) {
				TreePath path = e.getPath();
				BookItemNode node = (BookItemNode) path.getLastPathComponent();
				if (!node.isExplored()) {
					DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
					node.explore();
					model.nodeStructureChanged(node);
				}
			}

			@Override
			public void treeCollapsed(TreeExpansionEvent e) {
			}
		});

		tree.addTreeSelectionListener(new TreeSelectionListener() {
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				TreePath path = e.getPath();
				BookItemNode node = (BookItemNode) path.getLastPathComponent();
				BookItem bi = (BookItem) node.getUserObject();
				set.mvLast = bi.mv;
				pos.fromFen(bi.fen);
				panel.updateBoard();
			}
		});
		scrollPane.setViewportView(tree);
		panel.updateBoard();
	}

	public DefaultTreeModel createModel() {
		BookItem bi = new BookItem();
		pos.fromFen(Position.STARTUP_FEN[0]);
		bi.fen = pos.toFen();
		bi.zoblock = pos.zobristKey;
		BookItemNode bk = new BookItemNode(bi);
		bk.explore();
		return new DefaultTreeModel(bk);
	}

}
